//
//  HandleViolationViewController.swift
//  TrafficViolation
//
//  Created by clover on 2017/5/3.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
class HandleViolationController: BaseController {
    var handleTable:UITableView!//数据表格
    var handleArray = Array<CarModel>()
    var violationArray: [ViolationModel]!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupNavBar(title: "违章办理", isback: true, appearWay: 2, isright: false)//自定义导航
        violationArray = Array<ViolationModel>()
        //初始化表格
        handleTable = UITableView(frame: CGRect(x:0,y:kStatusBarH + kNavigationBarH,width:kScreenW,height:kScreenH),style: UITableViewStyle.plain)
        handleTable?.delegate = self
        handleTable?.dataSource = self
        handleTable?.backgroundColor = tableBgColor
        handleTable?.separatorStyle = UITableViewCellSeparatorStyle.none
        handleTable?.rowHeight = 85
        self.view.addSubview(handleTable!)
        handleTable?.snp.makeConstraints({ (make) in
            make.left.right.equalTo(self.view)
            make.top.equalTo(self.view).offset(kStatusBarH + kNavigationBarH)
            make.bottom.equalTo(self.view)
        })
        if reachability.isReachable {
            loadCarList()//查询车辆
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}
extension HandleViolationController:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return handleArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell: HandleViolationCell? = tableView.dequeueReusableCell(withIdentifier: "handleCellID") as? HandleViolationCell
        if cell == nil {
            cell = HandleViolationCell(style: UITableViewCellStyle.default,reuseIdentifier: "handleCellID")
        }
        if handleArray.count > 0 {
            let model = handleArray[(indexPath as NSIndexPath).row]
            cell?.configCellDateWithModel(model)
        }
        return cell!
    }
    //选中cell时触发这个代理
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        tableView.deselectRow(at: indexPath, animated: false)//点击完成，取消高亮
        let model = handleArray[(indexPath as NSIndexPath).row]
        let vlc = ViolationListController()
        vlc.carid = model.carid
        vlc.carnum = model.carnum
        self.navigationController?.present(vlc, animated: true, completion: nil)
    }
}
extension HandleViolationController{
    func loadCarList(){
        let urlString = "\(projectUrl)/car/carList/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)" //网络请求地址
        Alamofire.request(urlString, method: .get, parameters: nil, headers : headers).responseJSON { (response) in
            if response.result.isSuccess{
                let json = JSON(response.result.value as! [String : AnyObject])
                if json["success"] == true {
                    for (_,subJson) : (String,JSON) in json["rdata"] {
                        let model = CarModel.init(dic: subJson)
                        self.handleArray.append(model)
                    }
                    if(self.handleArray.count > 0){
                        self.handleTable.reloadData()
                    }
                    
                }else{
                    self.failOperNoticeFun(code: json["rcode"].rawString()!, message: json["rdata"].rawString()!)
                }
            }
            else{
                print(response.result.error!) //输出错误日志
            }
        }
    }
}

